﻿using Hjt212Parser;
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DataProcessorDust.EF;

namespace DataProcessorDust
{
    public partial class ServiceLampblackMonitor : ServiceBase
    {
        private static ILog Log = LogManager.GetLogger(typeof(ServiceLampblackMonitor));
        private int milliSeconds = 1000;
        public ServiceLampblackMonitor()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            new Thread(ProcessCmdNonHourOrDay).Start();
        }

        protected override void OnStop()
        {
        }

        private void ProcessCmdNonHourOrDay(object state)
        {
            while (true)
            {
                try
                {
                    using (var gCtx = new GatewayEntities())
                    using (var pCtx = new LampblackMonitorEntities())
                    {
                        var items = gCtx.received_msg.Where(t => t.is_length_right)
                            .Where(t => t.processed_flag.Equals(ProcessFlag.NotProcessed.ToString())).Take(1000)
                            .ToList();
                        foreach (var item in items)
                        {
                            item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                            switch (item.cn)
                            {
                                case "2011":
                                    ProcessorData.Process2011ForMonitor(pCtx, item.received_string);
                                    ProcessorData.Process2011(pCtx,item.received_string);
                                    break;
                                case "2051":
                                    ProcessorData.Process2051(pCtx, item.received_string);
                                    break;
                                case "2061":
                                    ProcessorData.Process2061(pCtx, item.received_string);
                                    break;
                                case "2031":
                                    ProcessorData.Process2031(pCtx, item.received_string);
                                    ProcessorData.Process2031ToMonth(pCtx, item.received_string);
                                    ProcessorData.Process2031ToYear(pCtx, item.received_string);

                                    break;
                            }
                        }
                        gCtx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
        }

    }
}
